home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 August: Tool Chest / Dev.CD Aug 98 TC.toast / Sample Code / Games / MoofWars / Tim's Libraries / TGraphic.h < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-19  |  9.5 KB  |  232 lines  |  [TEXT/CWIE]

  1. /*************************************************************************************
  2. #
  3. #    TGraphic.h
  4. #    
  5. #
  6. #
  7. #    The TGraphic class encapsulates custom blitting of a graphic to an offscreen GWorld.  The
  8. #    current blitters are based on the Encoded Sprite from "Tips of the Mac Gaming Gurus", with
  9. #    a number of additions and refinements.  For example, this class handles hit testing for a 
  10. #    single point, hit testing any two graphics against each other, and using the sprite data
  11. #    as a mask for copying from a "background" GWorld into the "foreground" GWorld.  This class
  12. #    also handles some alignment issues for the 604, which can slow down the normal TGraphic
  13. #    class.
  14. #    
  15. #    Assumptions:  Currently, this class assumes that the color table used to encode the sprites
  16. #    is loaded and being used for both GWorlds.  The GWorlds must have a bit depth of 8 -- no
  17. #    other bit depth will work properly.
  18. #    
  19. #    All TGraphics share the same destinations, which are set up with the "PrepareDrawing" and
  20. #    "EndDrawing" commands.
  21. #
  22. #    Note that this class isn't currently intended to be subclassed, so all of the methods are 
  23. #    non-virtual.  This might change if at some point we determine we need to create a subclass.
  24. #
  25. #    Author: Timothy Carroll
  26. #    Apple Developer Technical Support
  27. #    timc@apple.com
  28. #
  29. #    Modification History: 
  30. #
  31. #    6/1/96        TMC     Initial Release
  32. #
  33. #    Copyright © 1996 Apple Computer, Inc., All Rights Reserved
  34. #
  35. #
  36. #    You may incorporate this sample code into your applications without
  37. #    restriction, though the sample code has been provided "AS IS" and the
  38. #    responsibility for its operation is 100% yours.  However, what you are
  39. #    not permitted to do is to redistribute the source as "DSC Sample Code"
  40. #    after having made changes. If you're going to re-distribute the source,
  41. #    we require that you make it clear in the source that the code was
  42. #    descended from Apple Sample Code, but that you've made changes.
  43. #
  44. *************************************************************************************/
  45.  
  46. #ifndef _TGRAPHIC_
  47. #define _TGRAPHIC_
  48.  
  49. #pragma once
  50.  
  51. #include <QDOffscreen.h>
  52.  
  53. #if PRAGMA_ALIGN_SUPPORTED
  54. #pragma options align=power
  55. #endif
  56.  
  57. enum eDrawFlag
  58. {
  59.     kDrawGraphic = 0,
  60.     kDrawBackground = 1
  61. };
  62.  
  63. class TGraphic
  64.     {
  65.     public:
  66. /****************************************************************************************************
  67.     Static Creator and Reference Counting
  68.     
  69.     These are the routines that handle the actual creation of the objects, along with reference
  70.     counting and so on.  We don't provide macros here, like we did with the TGraphicCollection,
  71.     as these routines are mostly called from that other class and aren't really necessary.
  72.     
  73.     A TGraphic is currently defined by a resource of type 'Gfic'.
  74. ****************************************************************************************************/
  75.  
  76.     static TGraphic         *NewGraphic (SInt16 resID);
  77.  
  78.     void AddReference (void);
  79.     void DisposeReference (void);
  80.  
  81.     
  82. /****************************************************************************************************
  83.     Constructor/Destructor
  84.     
  85.     Note that the constructor and destructor should not (in general) be called by users.  In fact,
  86.     I might try to make them protected later, once I check to make sure that is possible.  All
  87.     creation and destruction is handled by the above routines and if you destroy objects yourself,
  88.     BAD THINGS can happen.
  89. ****************************************************************************************************/
  90.  
  91.     TGraphic (SInt16 resID);
  92.     ~TGraphic(void);
  93.  
  94.  
  95. /****************************************************************************************************
  96.     Locking and Unlocking
  97.     
  98.     You are never required to lock a graphic -- the routines run fine either way.  We provide these
  99.     functions because the main use of this class is for drawing sprites.  In a game, most of the
  100.     graphics will be allocated ahead of time and stick around until the game ends.  By locking them
  101.     down high, we ensure they won't need to be moved to make room for other handles that might need
  102.     to be allocated.
  103. ****************************************************************************************************/
  104.     OSErr    LockGraphic (void);
  105.     OSErr    UnlockGraphic (void);
  106.     
  107.     
  108. /****************************************************************************************************
  109.     Creating and destroying the graphic
  110.     
  111.     The actual work to create and destroy the collection is done in the following two routines.
  112.     CreateGraphic loads the resource, gets the data and creates any TGraphic objects necessary
  113.     for the collection.  DestroyGraphic simply reverses the process.
  114.     
  115.     In addition, a few utility functions are included for loading from different kinds of resources,
  116.     and actually encoding the graphic.
  117. ****************************************************************************************************/
  118.     OSErr    CreateGraphic (void);
  119.     OSErr    DestroyGraphic (void);
  120.  
  121.  
  122. /****************************************************************************************************
  123.     File Output
  124.     
  125.     These routines allow convenient writing of a graphic out to a resource fork, either in the
  126.     compressed format or as a PICT resource.
  127. ****************************************************************************************************/
  128.  
  129.     OSErr    WriteToGraphicResource (void);
  130.     OSErr    WriteToPICTResource (void);
  131. /****************************************************************************************************
  132.     Accessor Functions
  133.     
  134.     We define a few accessor functions to allow clients to get some information.  At this point, we
  135.     are advertising the TGraphics as something that can be obtained.  We do not immediatly force
  136.     a link to the TGraphic object when we send it to you.  If you want a persistant link to the
  137.     TGraphic object, you must create a link to it and properly dispose of it.  Otherwise, make sure
  138.     you don't destroy the TGraphic until you are done with the TGraphic.
  139. ****************************************************************************************************/
  140.  
  141. // We'll inline the these for speed.
  142.     SInt16        GetResID(void) {return fResID;}
  143.     Rect        GetBounds (void) {return fBounds;}        
  144.  
  145. /****************************************************************************************************
  146.     Utility Functions
  147.     
  148.     The meat and potatoes of the TGraphic class -- method calls to draw graphics and
  149.     perform hit testing.
  150.     
  151.     CopyImage accept a point in the local coordinate system of the destination PixMap.  If the
  152.     "useBackground" flag is set and a background PixMap was specified, it will draw from the
  153.     background to the destination, using the graphic's data only as a mask.  This allows a cheap
  154.     and easy "erase" function.
  155.     
  156.     HitTest accepts a point in coordinates local to the TGraphic's bounds rect, and returns true
  157.     if that point hits a "drawn" portion of the graphic.
  158.     
  159.     Intersect is a static function that accepts two TGraphic objects and the top left coordinate
  160.     for each object.  Both points should be in the same coordinate system. 
  161.     It returns true if the actual objects intersect each other.
  162. ****************************************************************************************************/
  163.     
  164.     void            CopyImage (SInt32 top, SInt32 left, Boolean useBackground);
  165.     Boolean            HitTest (SInt32 v, SInt32 h);
  166.     static Boolean    Intersect (TGraphic *object1, TGraphic *object2, SInt32 v1, SInt32 h1, SInt32 v2, SInt32 h2);
  167.     
  168.     
  169.     protected:
  170.  
  171.  
  172. /****************************************************************************************************
  173.     Protected and Internal Functions
  174.     
  175.     We have four internal functions that do all of the image drawing within the TGraphic object.
  176.     Basically, CopyImage determine whether or not we are clipped or not, and calls the appropriate
  177.     utility function to do the work.
  178. ****************************************************************************************************/
  179.  
  180.     OSErr   LoadFromGraphicResource (void);
  181.     OSErr   LoadFromPICTResource (void);
  182.     OSErr   LoadFromICN8Resource (void);
  183.     OSErr   LoadFromCIconResource (void);
  184.     
  185.     OSErr    EncodeGraphic (PixMapHandle theGraphic, Rect *encodeRect);
  186.  
  187.     void GraphicClipped (SInt32 top, SInt32 left, UInt32 clipLeft, UInt32 clipRight, UInt32 clipTop, UInt32 clipBottom);
  188.     void GraphicUnclipped (SInt32 top, SInt32 left);
  189.     void BackgroundClipped (SInt32 top, SInt32 left, UInt32 clipLeft, UInt32 clipRight, UInt32 clipTop, UInt32 clipBottom);
  190.     void BackgroundUnclipped (SInt32 top, SInt32 left);
  191.  
  192.     // These will probably never be used in a game, but are useful for dumping them out
  193.     // to a file.  For this reason, we only create unclipped version of the routines.
  194.     void DrawMaskUnclipped (SInt32 top, SInt32 left);
  195.     void HitMaskUnclipped (SInt32 top, SInt32 left);
  196.  
  197.  
  198. /****************************************************************************************************
  199.     Data Structures
  200.     
  201.     The actual data used to hold the TGraphic's data.  We make sure that the data is aligned
  202.     properly for both PowerPC and 68K struct alignment.
  203.     
  204.     fBounds is a zero coordinate based rectangle that holds the actual size of the sprite.  We
  205.     get this from the sprite data when we load it and use it in all other routines.
  206. ****************************************************************************************************/
  207.     
  208.     
  209.     Rect            fBounds;              // 8 bytes
  210.  
  211. // This handle holds the data the TGraphic class uses to draw itself
  212.     Handle            fImage;                // 4 bytes
  213. // This handle holds the data the TGraphic class uses for hit testing
  214.  
  215.     Handle            fHitMask;            // 4 bytes
  216.  
  217.     SInt16            fResID;             // 2 bytes
  218.     
  219.     UInt16            fReferenceCount;    // 2 bytes
  220.         
  221.     UInt16            fBitDepth;            // 2 bytes
  222.         
  223.     UInt16            fFlags;                // 2 bytes
  224.     
  225. };
  226.  
  227.  
  228. #if PRAGMA_ALIGN_SUPPORTED
  229. #pragma options align=reset
  230. #endif
  231.  
  232. #endif /* _TGRAPHIC_ */